package com.egao.common.publicly;


import cn.hutool.core.bean.BeanUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.SelectedColumn;
import com.egao.common.module.cms.entity.vo.SelectedColumnListVO;
import com.egao.common.module.cms.entity.vo.SelectedColumnVO;
import com.egao.common.module.cms.service.SelectedColumnService;
import com.egao.common.publicly.entity.ArticleVO;
import com.egao.common.publicly.service.PubliclyArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 选调专栏
 * @author dl
 */
@Api(tags = "选调专栏")
@RestController
@RequestMapping("${api}")
public class PubliclySelectColumnController {
    @Autowired
    private SelectedColumnService selectedColumnService;

    @ApiOperation("选调专栏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/selected-column/list")
    public PageResult<SelectedColumnListVO> list(HttpServletRequest request){
        PageParam<SelectedColumnListVO> pageParam = new PageParam<>(request);
        return new PageResult<>(selectedColumnService.listPage(pageParam),pageParam.getTotal());
    }


    @ApiOperation("选调专栏详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "选调专栏id", required = true, dataType = "int", paramType = "query"),
    })
    @OperLog(value = "选调专栏", desc = "查询详情")
    @GetMapping("/publicly/selected-column/details")
    public JsonResult get(Integer id){
        if(id == null){
            return JsonResult.error("没有选调专栏id");
        }

        SelectedColumn selectedColumn = selectedColumnService.getSelectedColumnById(id);
        Integer hits = selectedColumn.getHits();
        hits += 1;
        selectedColumn.setHits(hits);
        selectedColumnService.updateSelectedColumn(selectedColumn);
        SelectedColumnVO selectedColumnVO = new SelectedColumnVO();
        BeanUtil.copyProperties(selectedColumn ,selectedColumnVO);
        return JsonResult.ok().setData(selectedColumnVO);
    }

}
